/*
 * @lc app=leetcode.cn id=938 lang=java
 *
 * [938] 二叉搜索树的范围和
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class RangeSumBST {
  public int rangeSumBST(TreeNode root, int low, int high) {
    if (root == null) {
      return 0;
    }
    if (root.val < low) {
      return rangeSumBST(root.right, low, high);
    } else if (root.val > high) {
      return rangeSumBST(root.left, low, high);
    } else {
      return root.val + rangeSumBST(root.left, low, high) + rangeSumBST(root.right, low, high);
    }
  }
}
// @lc code=end
